-- region HeroCellWithMask.lua
-- Author : kevinz
-- Date   : 2015/04/09
-- Description:英雄框(带圆形遮罩)通用控件 参见\shenqi\mobile\SangoHero\素材\通用英雄框控件\hero_cell_with_mask.fla

require "tagMap/Tag_hero_cell_with_mask"

HeroCellWithMask = class("HeroCellWithMask",function()
    local ret = cc.Node:create()

    -- 英雄模型ID
    ret.nHeroID = nil

    -- 站位按钮 可以注册点击事件
    ret.btn_place = nil

    -- 英雄头像图标
    ret.img_hero_portrait = nil

    -- 点击事件回调
    ret.onClickHeroItem = nil

    return ret
end)

HeroCellWithMask.__index = HeroCellWithMask

-- 创建并且替换现有的node
function HeroCellWithMask:createAndReplaceNode(node)
    local ret = HeroCellWithMask:create()
    replaceNode(ret,node)
    return ret
end

function HeroCellWithMask:create()
    local ret = HeroCellWithMask:new()
    ret:init()
    return ret
end

function HeroCellWithMask:init()
    TuiManager:getInstance():parseScene(self,"panel_hero_cell_with_mask",PATH_HERO_CELL_WITH_MASK)
    self.btn_place = findChildByTag(self,Tag_hero_cell_with_mask.BTN_PLACE)
    self.img_hero_portrait = findChildByTag(self,Tag_hero_cell_with_mask.IMG_HERO_PLACE_80)
    self.img_hero_portrait:setScale(0.85)
    self.btn_place:setVisible(false)

    -- 点击事件
    local function onClickHeroItem(pSender)
		if nil ~= self.onClickHeroItem then
			self.onClickHeroItem(p_sender,self.nHeroID)
		end
    end  
    self.btn_place:setOnClickScriptHandler(onClickHeroItem)
    self.btn_place:setSyncScaleParent(false)
end

-- 设置点击事件回调
function HeroCellWithMask:setClickCallback(onClickFunc)
    self.onClickHeroItem = onClickFunc
end

-- 设置是否可点击
function HeroCellWithMask:setClickEnabled(bEnable)
    self.btn_place:setEnabled(bEnable)
end

-- 设置头像框是否具有缩放效果
function HeroCellWithMask:setClickScaleEnable(bEnable)
    self.btn_place:setSyncScaleParent(bEnable)
end

-- 设置英雄头像框
function HeroCellWithMask:setHeroPortrait(nHeroID)
    self.nHeroID = nHeroID
    --self.img_hero_portrait:setSpriteFrame(self:createHeroPortrait130(nHeroID))
    replaceWithHeroCircle(self.img_hero_portrait,nHeroID)
end

-- 创建80*80的英雄头像 此时从道具表取资源
function HeroCellWithMask:createHeroPortrait130(nHeroID)
	local sPath = string.format("res/portrait/%d.png",nHeroID)
    local objHeroFrame = getFrame(sPath)
    if nil == objHeroFrame then
    	log.error("HeroCellWithMask:createHeroPortrait130","no resource: "..sPath)
    	sPath = string.format("res/portrait/%d.png",0)
	end

	local objSprite = cc.Sprite:createWithSpriteFrameName(sPath)
	return maskSprite(objSprite,"res/public/frame/img_mask_hero_80.png"):getSpriteFrame()
end